package com.kbbywyclient.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kbbywyclient.R;


/**
 * Created by FHW
 * on 2017/3/24
 */
public class CustomToolBar extends Toolbar {
    //添加布局必不可少的工具
    private LayoutInflater mInflater;
    //标题
    private TextView mTextTitle;
    private String tilte = "";
    //左边按钮
    private ImageView mLeftView;
    private TextView tv_left_text;

    private Drawable leftIcon = null;
    private String leftText = "";

    //右边按钮
    private ImageView mRightView;
    private TextView tv_right_text;

    private Drawable rightIcon = null;
    private String rightText = "";

    private View mView;
    private View viewLeft;
    private View viewRight;

    //以下是继承ToolBar必须创建的三个构造方法
    public CustomToolBar(Context context) {
        this(context, null);
    }

    public CustomToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();

        //Set the content insets for this toolbar relative to layout direction.
        //setContentInsetsRelative(10, 10);

        if (attrs != null) {
            //读写自定义的属性，如果不会自己写的时候，就看看官方文档是怎么写的哈
            /**
             * 下面是摘自官方文档
             * final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
             R.styleable.Toolbar, defStyleAttr, 0);

             mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
             mSubtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
             mGravity = a.getInteger(R.styleable.Toolbar_android_gravity, mGravity);
             mViewGravity = Gravity.TOP;
             mTitleMarginStart = mTitleMarginEnd = mTitleMarginTop = mTitleMarginBottom =
             a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargins, 0);

             final int marginStart = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginStart, -1);
             if (marginStart >= 0) {
             mTitleMarginStart = marginStart;
             }

             *
             */
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.CustomToolBar, defStyleAttr, 0);


            //左边
            leftIcon = a.getDrawable(R.styleable.CustomToolBar_leftViewIcon);
            if (leftIcon != null) {
                setLeftViewIcon(leftIcon);
            }
            leftText = a.getString(R.styleable.CustomToolBar_leftText);
            if(leftText!=null) {
                if (!leftText.isEmpty()) {
                    setLeftText(leftText);
                }
            }
            //右边
            rightIcon = a.getDrawable(R.styleable.CustomToolBar_rightViewIcon);
            if (rightIcon != null) {
                setRightViewIcon(rightIcon);
            }
            rightText = a.getString(R.styleable.CustomToolBar_rightText);
            if(rightText!=null) {
                if (!rightText.isEmpty()) {
                    setRightText(rightText);
                }
            }

            tilte = a.getString(R.styleable.CustomToolBar_toolBartitle);
            if(tilte!=null) {
                if (!tilte.isEmpty()) {
                    setTitle(tilte);
                }
            }
            boolean isShowTitle = a.getBoolean(R.styleable.CustomToolBar_isShowTitle, true);

            //如果要显示title的时候
            if (!isShowTitle) {
                hideTitleView();
            }
            //资源的回收
            a.recycle();
        }

    }

    private void initView() {

        if (mView == null) {
            //初始化
            mInflater = LayoutInflater.from(getContext());
            //添加布局文件
            mView = mInflater.inflate(R.layout.custom_toolbar, null);
            mTextTitle = (TextView) mView.findViewById(R.id.tv_title);
            //左边
            mLeftView = (ImageView) mView.findViewById(R.id.iv_left_icon);
            tv_left_text = (TextView) mView.findViewById(R.id.tv_left_text);
            viewLeft = mView.findViewById(R.id.rl_left);
            //右边
            mRightView = (ImageView) mView.findViewById(R.id.iv_right_icon);
            tv_right_text = (TextView) mView.findViewById(R.id.tv_right_text);
            viewRight = mView.findViewById(R.id.rl_right);

            //然后使用LayoutParams把控件添加到子view中
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            addView(mView, lp);
//            setBackgroundColor(getResources().getColor(R.color.title_color));
        }
    }

    private void setTitle(String titleStr) {
        if (mTextTitle != null) {
            mTextTitle.setText(titleStr);
            showTitleView();
        }
    }

    @Override
    public void setTitle(int resId) {
        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (mTextTitle != null) {
            mTextTitle.setText(title);
            showTitleView();
        }
    }

    //隐藏标题
    public void hideTitleView() {
        if (mTextTitle != null)
            mTextTitle.setVisibility(GONE);
    }

    //显示标题
    public void showTitleView() {
        if (mTextTitle != null)
            mTextTitle.setVisibility(VISIBLE);
    }

    //给右侧按钮设置图片，也可以在布局文件中直接引入
    // 如：app:leftViewIcon="@drawable/icon_back_32px"
    public void setRightViewIcon(Drawable icon) {
        if (mRightView != null) {
            mRightView.setImageDrawable(icon);
            mRightView.setVisibility(VISIBLE);
        }
    }

    private void setRightText(String rightText) {
        tv_right_text.setVisibility(VISIBLE);
        tv_right_text.setText(rightText);
    }

    //给左侧按钮设置图片，也可以在布局文件中直接引入
    private void setLeftViewIcon(Drawable icon) {
        if (mLeftView != null) {
            mLeftView.setImageDrawable(icon);
            mLeftView.setVisibility(VISIBLE);
        }
    }

    private void setLeftText(String leftText) {
        tv_left_text.setVisibility(VISIBLE);
        tv_left_text.setText(leftText);
    }

    //设置右侧按钮监听事件
    public void setRightViewOnClickLinster(OnClickListener linster) {
        viewRight.setOnClickListener(linster);
    }

    //设置左侧按钮监听事件
    public void setLeftViewOnClickLinster(OnClickListener linster) {
        viewLeft.setOnClickListener(linster);
    }
}
